<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
    </el-row>
    <!--  表格内容  -->
    <el-table v-loading="loading" :data="infoList">
      <el-table-column type="selection" width="55" align="center"/>
      <!--  productCode   -->
      <el-table-column label="商品编码" align="center" prop="productCode" :show-overflow-tooltip="true"/>
      <!--   productName   -->
      <el-table-column label="商品名称" align="center" prop="productName" :show-overflow-tooltip="true"/>
      <!--   categoryCode   -->
      <el-table-column label="商品分类" align="center" prop="categoryCode">
        <template slot-scope="scope">
          <span>{{ scope.row.baseCate.categoryName }}</span>
        </template>
      </el-table-column>
      <!--   createTime   -->
      <el-table-column label="创建时间" align="center" prop="createTime" width="280"/>
    </el-table>
    <!--分页插件-->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getInfoList"
    />
    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="商品名称" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入商品名称"/>
        </el-form-item>
        <el-form-item label="商品类别" prop="categoryCode">
          <el-select
            v-model="form.categoryCode"
            placeholder="商品类别"
            clearable
            style="width: 240px"
          >
            <el-option
              v-for="cate in categoryList"
              :key="cate.categoryCode"
              :label="cate.categoryName"
              :value="cate.categoryCode"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {getProductList, addProduct} from "@/api/base/Product";
import {getCategoryList} from "@/api/base/Cate";

export default {
  name: 'Info',
  data() {
    return {
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      action: "",
      open: false,
      // 遮罩层
      loading: true,
      // 商品信息
      infoList: [],
      // 总条数
      total: 0,
      // 分页参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 新增需要发送到后端的数据对象
      form: {
        productName: "",
        categoryCode: undefined
      },
      categoryList: [],
    }
  },
  created() {
    this.getInfoList();
  },
  methods: {
    // 获取商品信息
    async getInfoList() {
      this.loading = true
      const data = await getProductList({}, this.queryParams);
      this.total = data.total;
      this.infoList = data.rows;
      this.loading = false
    },
    async handleAdd() {
      this.title = "新增商品";
      this.action = "add";
      await this.queryCategoryList();
      this.open = true;
    },
    async queryCategoryList() {
      const data = await getCategoryList();
      this.categoryList = data.data;
    },
    // 表单重置
    reset() {
      this.form = {
        productName: "",
        categoryCode: undefined
      }
      this.resetForm("form")
    },
    // 提交表单
    async submitForm() {
      this.$refs["form"].validate(async valid => {
        if (valid) {
          // 新增商品
          if (this.action === "add") {
            const data = await addProduct(this.form);
            this.$modal.msgSuccess(data.msg);
            this.open = false
            await this.getInfoList();
          } else {
            // 修改商品
          }
        }
      });
    },
    // 取消
    cancel() {
      this.open = false;
      this.reset();
    }
  }
}
</script>


